using MaterialSkin.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;
using VideoAutoGenerator.Core;

namespace VideoAutoGenerator.UI.UserControls
{
    public partial class FontSettingsControl : MaterialSkin.Controls.MaterialCard
    {
        private MaterialLabel firstRowFontSizeLabel;
        private NumericUpDown firstRowFontSizeNumericUpDown;
        private MaterialLabel firstRowFontColorLabel;
        private MaterialButton firstRowFontColorButton;

        private MaterialLabel firstRowFontFamilyLabel;
        private ComboBox firstRowFontFamilyComboBox;

        private MaterialLabel firstRowFontStyleLabel;
        private ComboBox firstRowFontStyleComboBox;

        private MaterialLabel secondRowFontSizeLabel;
        private NumericUpDown secondRowFontSizeNumericUpDown;
        private MaterialLabel secondRowFontColorLabel;
        private MaterialButton secondRowFontColorButton;

        private MaterialLabel secondRowFontFamilyLabel;
        private ComboBox secondRowFontFamilyComboBox;

        private MaterialLabel secondRowFontStyleLabel;
        private ComboBox secondRowFontStyleComboBox;

        private MaterialLabel textFontSizeLabel;
        private NumericUpDown textFontSizeNumericUpDown;
        private MaterialLabel textFontFamilyLabel;
        private ComboBox textFontFamilyComboBox;
        private MaterialLabel textColorLabel;
        private MaterialButton textColorButton;
        private MaterialLabel textBackgroundColorLabel;
        private MaterialButton textBackgroundColorButton;
        private MaterialLabel highlightBackgroundColorLabel;
        private MaterialButton highlightBackgroundColorButton;

        public FontSettingsControl()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();

            // 创建控件
            CreateControls();

            // 设置控件位置和样式
            SetupLayout();
            
            // 添加事件处理程序
            SetupEventHandlers();

            this.ResumeLayout(false);
        }

        private void CreateControls()
        {
            // 第一行字体样式
            firstRowFontSizeLabel = new MaterialLabel { Text = "大小:" };
            firstRowFontSizeNumericUpDown = new NumericUpDown { Minimum = 0, Maximum = 500, Value = 128 };
            firstRowFontColorLabel = new MaterialLabel { Text = "颜色:" };
            firstRowFontColorButton = new MaterialButton { Width = 40, Text = "选择" };

            firstRowFontFamilyLabel = new MaterialLabel { Text = "字体:" };
            firstRowFontFamilyComboBox = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList };
            firstRowFontFamilyComboBox.Items.AddRange(new string[] { "Adobe Arabic", "Arial", "Times New Roman", "Microsoft YaHei" });

            firstRowFontStyleLabel = new MaterialLabel { Text = "样式:" };
            firstRowFontStyleComboBox = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList };
            firstRowFontStyleComboBox.Items.AddRange(new string[] { "normal", "italic", "bold" });

            // 第二行字体样式
            secondRowFontSizeLabel = new MaterialLabel { Text = "大小:" };
            secondRowFontSizeNumericUpDown = new NumericUpDown { Minimum = 0, Maximum = 500, Value = 72 };
            secondRowFontColorLabel = new MaterialLabel { Text = "颜色:" };
            secondRowFontColorButton = new MaterialButton { Width = 40, Text = "选择" };

            secondRowFontFamilyLabel = new MaterialLabel { Text = "字体:" };
            secondRowFontFamilyComboBox = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList };
            secondRowFontFamilyComboBox.Items.AddRange(new string[] { "Adobe Arabic", "Arial", "Times New Roman", "Microsoft YaHei" });

            secondRowFontStyleLabel = new MaterialLabel { Text = "样式:" };
            secondRowFontStyleComboBox = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList };
            secondRowFontStyleComboBox.Items.AddRange(new string[] { "normal", "italic", "bold" });

            // 文本样式
            textFontSizeLabel = new MaterialLabel { Text = "大小:" };
            textFontSizeNumericUpDown = new NumericUpDown { Minimum = 0, Maximum = 500, Value = 48 };
            textFontFamilyLabel = new MaterialLabel { Text = "字体:" };
            textFontFamilyComboBox = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList };
            textFontFamilyComboBox.Items.AddRange(new string[] { "Arial", "Times New Roman", "Microsoft YaHei" });
            textColorLabel = new MaterialLabel { Text = "颜色:" };
            textColorButton = new MaterialButton { Width = 40, Text = "选择" };
            textBackgroundColorLabel = new MaterialLabel { Text = "背景色:" };
            textBackgroundColorButton = new MaterialButton { Width = 40, Text = "选择" };
            highlightBackgroundColorLabel = new MaterialLabel { Text = "高亮背景色:" };
            highlightBackgroundColorButton = new MaterialButton { Width = 40, Text = "选择" };
        }

        private void SetupLayout()
        {
            var tableLayout = new TableLayoutPanel();
            tableLayout.AutoScroll = true;
            tableLayout.Dock = DockStyle.Fill;
            tableLayout.ColumnCount = 4;
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            tableLayout.RowCount = 16;
            for (int i = 0; i < 16; i++)
            {
                tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }

            // 第一行字体样式
            tableLayout.Controls.Add(new Label { Text = "第一行字体样式:" }, 0, 0);
            tableLayout.SetColumnSpan(new Label { Text = "第一行字体样式:" }, 4);

            tableLayout.Controls.Add(firstRowFontSizeLabel, 0, 1);
            tableLayout.Controls.Add(firstRowFontSizeNumericUpDown, 1, 1);
            tableLayout.Controls.Add(firstRowFontColorLabel, 2, 1);
            tableLayout.Controls.Add(firstRowFontColorButton, 3, 1);

            tableLayout.Controls.Add(firstRowFontFamilyLabel, 0, 2);
            tableLayout.Controls.Add(firstRowFontFamilyComboBox, 1, 2);
            tableLayout.SetColumnSpan(firstRowFontFamilyComboBox, 2);

            tableLayout.Controls.Add(firstRowFontStyleLabel, 0, 3);
            tableLayout.Controls.Add(firstRowFontStyleComboBox, 1, 3);
            tableLayout.SetColumnSpan(firstRowFontStyleComboBox, 2);

            // 第二行字体样式
            tableLayout.Controls.Add(new Label { Text = "第二行字体样式:" }, 0, 4);
            tableLayout.SetColumnSpan(new Label { Text = "第二行字体样式:" }, 4);

            tableLayout.Controls.Add(secondRowFontSizeLabel, 0, 5);
            tableLayout.Controls.Add(secondRowFontSizeNumericUpDown, 1, 5);
            tableLayout.Controls.Add(secondRowFontColorLabel, 2, 5);
            tableLayout.Controls.Add(secondRowFontColorButton, 3, 5);

            tableLayout.Controls.Add(secondRowFontFamilyLabel, 0, 6);
            tableLayout.Controls.Add(secondRowFontFamilyComboBox, 1, 6);
            tableLayout.SetColumnSpan(secondRowFontFamilyComboBox, 2);

            tableLayout.Controls.Add(secondRowFontStyleLabel, 0, 7);
            tableLayout.Controls.Add(secondRowFontStyleComboBox, 1, 7);
            tableLayout.SetColumnSpan(secondRowFontStyleComboBox, 2);

            // 文本样式
            tableLayout.Controls.Add(new Label { Text = "文本样式:" }, 0, 8);
            tableLayout.SetColumnSpan(new Label { Text = "文本样式:" }, 4);

            tableLayout.Controls.Add(textFontSizeLabel, 0, 9);
            tableLayout.Controls.Add(textFontSizeNumericUpDown, 1, 9);
            tableLayout.Controls.Add(textColorLabel, 2, 9);
            tableLayout.Controls.Add(textColorButton, 3, 9);

            tableLayout.Controls.Add(textFontFamilyLabel, 0, 10);
            tableLayout.Controls.Add(textFontFamilyComboBox, 1, 10);
            tableLayout.SetColumnSpan(textFontFamilyComboBox, 2);

            tableLayout.Controls.Add(textBackgroundColorLabel, 0, 11);
            tableLayout.Controls.Add(textBackgroundColorButton, 1, 11);

            tableLayout.Controls.Add(highlightBackgroundColorLabel, 2, 11);
            tableLayout.Controls.Add(highlightBackgroundColorButton, 3, 11);

            this.Controls.Add(tableLayout);
        }
        
        private void SetupEventHandlers()
        {
            // 为颜色选择按钮添加事件处理程序
            firstRowFontColorButton.Click += (sender, e) => SelectColor(firstRowFontColorButton);
            secondRowFontColorButton.Click += (sender, e) => SelectColor(secondRowFontColorButton);
            textColorButton.Click += (sender, e) => SelectColor(textColorButton);
            textBackgroundColorButton.Click += (sender, e) => SelectColor(textBackgroundColorButton);
            highlightBackgroundColorButton.Click += (sender, e) => SelectColor(highlightBackgroundColorButton);
        }
        
        // 颜色选择对话框
        private void SelectColor(MaterialButton button)
        {
            using (ColorDialog colorDialog = new ColorDialog())
            {
                if (colorDialog.ShowDialog() == DialogResult.OK)
                {
                    // 将选中的颜色以十六进制格式存储在按钮的Tag属性中
                    button.Tag = ColorTranslator.ToHtml(colorDialog.Color);
                    // 更新按钮背景色以便用户可视化识别
                    button.BackColor = colorDialog.Color;
                }
            }
        }
        
        // 从配置管理器加载配置数据到控件
        public void LoadConfig()
        {
            var config = ConfigManager.Instance;
            
            // 加载第一行字体样式配置
            var firstLineFontStyle = config.GetObject("projectSettings.firstLineFontStyle");
            if (firstLineFontStyle != null)
            {
                firstRowFontSizeNumericUpDown.Value = firstLineFontStyle["fontSize"]?.ToObject<int>() ?? 128;
                SetComboBoxSelectedItem(firstRowFontFamilyComboBox, firstLineFontStyle["fontFamily"]?.ToString(), "Adobe Arabic");
                SetComboBoxSelectedItem(firstRowFontStyleComboBox, firstLineFontStyle["fontStyle"]?.ToString(), "normal");
                
                // 加载颜色设置
                var color = firstLineFontStyle["color"]?.ToString();
                if (!string.IsNullOrEmpty(color))
                {
                    firstRowFontColorButton.Tag = color;
                    try
                    {
                        firstRowFontColorButton.BackColor = ColorTranslator.FromHtml(color);
                    }
                    catch
                    {
                        firstRowFontColorButton.BackColor = Color.Black;
                    }
                }
            }
            
            // 加载第二行字体样式配置
            var secondLineFontStyle = config.GetObject("projectSettings.secondLineFontStyle");
            if (secondLineFontStyle != null)
            {
                secondRowFontSizeNumericUpDown.Value = secondLineFontStyle["fontSize"]?.ToObject<int>() ?? 72;
                SetComboBoxSelectedItem(secondRowFontFamilyComboBox, secondLineFontStyle["fontFamily"]?.ToString(), "Adobe Arabic");
                SetComboBoxSelectedItem(secondRowFontStyleComboBox, secondLineFontStyle["fontStyle"]?.ToString(), "italic");
                
                // 加载颜色设置
                var color = secondLineFontStyle["color"]?.ToString();
                if (!string.IsNullOrEmpty(color))
                {
                    secondRowFontColorButton.Tag = color;
                    try
                    {
                        secondRowFontColorButton.BackColor = ColorTranslator.FromHtml(color);
                    }
                    catch
                    {
                        secondRowFontColorButton.BackColor = Color.Black;
                    }
                }
            }
            
            // 加载文本样式配置
            var textStyles = config.GetObject("projectSettings.textStyles");
            if (textStyles != null)
            {
                textFontSizeNumericUpDown.Value = textStyles["fontSize"]?.ToObject<int>() ?? 48;
                SetComboBoxSelectedItem(textFontFamilyComboBox, textStyles["fontFamily"]?.ToString(), "Arial");
                
                // 加载颜色设置
                var color = textStyles["color"]?.ToString();
                if (!string.IsNullOrEmpty(color))
                {
                    textColorButton.Tag = color;
                    try
                    {
                        textColorButton.BackColor = ColorTranslator.FromHtml(color);
                    }
                    catch
                    {
                        textColorButton.BackColor = Color.Black;
                    }
                }
                
                // 加载背景色设置
                var backgroundColor = textStyles["backgroundColor"]?.ToString();
                if (!string.IsNullOrEmpty(backgroundColor))
                {
                    textBackgroundColorButton.Tag = backgroundColor;
                    try
                    {
                        textBackgroundColorButton.BackColor = ColorTranslator.FromHtml(backgroundColor);
                    }
                    catch
                    {
                        textBackgroundColorButton.BackColor = Color.Black;
                    }
                }
                
                // 加载高亮背景色设置
                var highlightBackgroundColor = textStyles["highlightBackgroundColor"]?.ToString();
                if (!string.IsNullOrEmpty(highlightBackgroundColor))
                {
                    highlightBackgroundColorButton.Tag = highlightBackgroundColor;
                    try
                    {
                        highlightBackgroundColorButton.BackColor = ColorTranslator.FromHtml(highlightBackgroundColor);
                    }
                    catch
                    {
                        highlightBackgroundColorButton.BackColor = Color.Black;
                    }
                }
            }
        }
        
        // 从控件保存配置数据到配置管理器
        public void SaveConfig()
        {
            var config = ConfigManager.Instance;
            
            // 保存第一行字体样式配置
            config.SetValue("projectSettings.firstLineFontStyle.fontSize", (int)firstRowFontSizeNumericUpDown.Value);
            if (firstRowFontFamilyComboBox.SelectedItem != null)
                config.SetValue("projectSettings.firstLineFontStyle.fontFamily", firstRowFontFamilyComboBox.SelectedItem.ToString());
            if (firstRowFontStyleComboBox.SelectedItem != null)
                config.SetValue("projectSettings.firstLineFontStyle.fontStyle", firstRowFontStyleComboBox.SelectedItem.ToString());
            if (firstRowFontColorButton.Tag != null)
                config.SetValue("projectSettings.firstLineFontStyle.color", firstRowFontColorButton.Tag.ToString());
            
            // 保存第二行字体样式配置
            config.SetValue("projectSettings.secondLineFontStyle.fontSize", (int)secondRowFontSizeNumericUpDown.Value);
            if (secondRowFontFamilyComboBox.SelectedItem != null)
                config.SetValue("projectSettings.secondLineFontStyle.fontFamily", secondRowFontFamilyComboBox.SelectedItem.ToString());
            if (secondRowFontStyleComboBox.SelectedItem != null)
                config.SetValue("projectSettings.secondLineFontStyle.fontStyle", secondRowFontStyleComboBox.SelectedItem.ToString());
            if (secondRowFontColorButton.Tag != null)
                config.SetValue("projectSettings.secondLineFontStyle.color", secondRowFontColorButton.Tag.ToString());
            
            // 保存文本样式配置
            config.SetValue("projectSettings.textStyles.fontSize", (int)textFontSizeNumericUpDown.Value);
            if (textFontFamilyComboBox.SelectedItem != null)
                config.SetValue("projectSettings.textStyles.fontFamily", textFontFamilyComboBox.SelectedItem.ToString());
            if (textColorButton.Tag != null)
                config.SetValue("projectSettings.textStyles.color", textColorButton.Tag.ToString());
            if (textBackgroundColorButton.Tag != null)
                config.SetValue("projectSettings.textStyles.backgroundColor", textBackgroundColorButton.Tag.ToString());
            if (highlightBackgroundColorButton.Tag != null)
                config.SetValue("projectSettings.textStyles.highlightBackgroundColor", highlightBackgroundColorButton.Tag.ToString());
            
            config.SaveConfig();
        }
        
        // 辅助方法：设置ComboBox选中项
        private void SetComboBoxSelectedItem(ComboBox comboBox, string value, string defaultValue)
        {
            if (!string.IsNullOrEmpty(value) && comboBox.Items.Contains(value))
            {
                comboBox.SelectedItem = value;
            }
            else
            {
                comboBox.SelectedItem = defaultValue;
            }
        }
    }
}